package carmesi.wizards;

import java.awt.Component;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.openide.WizardDescriptor;
import org.openide.util.HelpCtx;

/**
 *
 * @author Victor
 */
public class CarmesiComponent extends JPanel implements WizardDescriptor.Panel<WizardDescriptor> {
    private Set<ChangeListener> listeners = new CopyOnWriteArraySet<ChangeListener>();
    private ComponentValidator validator;
    private WizardDescriptor wizardDescriptor;
    
    public CarmesiComponent(WizardDescriptor descriptor){
        wizardDescriptor=descriptor;
    }
    
    public ComponentValidator getValidator() {
        return validator;
    }

    public void setComponentValidator(ComponentValidator validator) {
        this.validator = validator;
    }

    @Override
    public Component getComponent() {
        return this;
    }

    @Override
    public HelpCtx getHelp() {
        return HelpCtx.DEFAULT_HELP;
    }

    @Override
    public void readSettings(WizardDescriptor data) {
        
    }

    @Override
    public void storeSettings(WizardDescriptor data) {
        
    }

    @Override
    public boolean isValid() {
        boolean isValid=true;
        if(validator != null){
            try {
                validator.validate(this);
            } catch (IllegalArgumentException ex) {
                if(wizardDescriptor != null){
                    wizardDescriptor.createNotificationLineSupport().setErrorMessage(ex.getMessage());
                }
                isValid = false;
            }
        }
        return isValid;
    }

    @Override
    public void addChangeListener(ChangeListener l) {
        listeners.add(l);
    }
    
    @Override
    public void removeChangeListener(ChangeListener l) {
        listeners.remove(l);
    }
    
    public void fireChangeEvent() {
        Iterator<ChangeListener> it=listeners.iterator();
        ChangeEvent ev = new ChangeEvent(this);
        while (it.hasNext()) {
            it.next().stateChanged(ev);
        }
    }
    
}
